package com.jiangchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiangchuang.domain.entity.*;
import com.jiangchuang.domain.response.GetReviewRes;
import com.jiangchuang.mapper.*;
import com.jiangchuang.service.IDishService;
import com.jiangchuang.service.IUserService;
import com.jiangchuang.utils.JwtUtils;
import com.jiangchuang.utils.MinIoUtil;
import com.jiangchuang.utils.UserContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAdminMapper adminMapper;
    @Autowired
    private BannedUserMapper bannedUserMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private IDishService dishService;
    @Autowired
    private DishTypeMapper dishTypeMapper;
    @Autowired
    private FanUserMapper fanUserMapper;
    @Autowired
    private RiceDetailMapper riceDetailMapper;
    @Autowired
    private KitchenMapper kitchenMapper;

    @Override
    public String userLogin(String username, String password) {
        // 获取用户信息
        QueryWrapper<UserAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", password);
        UserAdmin admin = adminMapper.selectOne(wrapper);
        String token="";

        if(admin!=null) {
            // 管理员是否存在，存在则返回token
            token = JwtUtils.getToken(admin.getId().toString());
        }else{
            throw new RuntimeException("账号或密码错误");
        }
        return token;
    }

    @Override
    public void addReviewNum(Integer num, Integer type) {
        UserAdmin admin = UserContextUtils.getCurrentUser();

        if(type==0){
            admin.setReviewImg(admin.getReviewImg()+num);
            admin.setCurrentReview(admin.getCurrentReview()+num);
            adminMapper.updateById(admin);
        }

    }

    @Override
    public User getUserById(Integer uid) {
        return userMapper.selectById(uid);
    }

    @Override
    public GetReviewRes getReviewNum(Integer adminId) {
        if(adminId==null){
            adminId = UserContextUtils.getCurrentUserId();
        }
        UserAdmin admin = adminMapper.selectById(adminId);
        GetReviewRes res = new GetReviewRes();
        res.setReviewImg(admin.getReviewImg());
        res.setCurrentReview(admin.getCurrentReview());
        return res;
    }

    @Override
    public void resetCurrentReview(Integer adminId) {
        if(adminId==null){
            adminId = UserContextUtils.getCurrentUserId();
        }
        UserAdmin admin = adminMapper.selectById(adminId);
        admin.setCurrentReview(0);
        adminMapper.updateById(admin);
    }

    @Override
    public Integer editUserCoin(Integer uid, String ope, Integer coinNum) {
        if(uid==null){
            uid = UserContextUtils.getCurrentUserId();
        }
        User user = userMapper.selectById(uid);
        user.setCoin(Objects.equals(ope, "add")?user.getCoin()+coinNum:user.getCoin()-coinNum);
        userMapper.updateById(user);
        return user.getCoin();
    }

    @Override
    public PageInfo<RiceDetail> getRiceDetail(Integer uid, int pageNo, int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<RiceDetail> pageInfo ;
        QueryWrapper<RiceDetail> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        if(uid!=null){
            wrapper.eq("uid", uid);
        }
        pageInfo = new PageInfo<>(riceDetailMapper.selectList(wrapper));

        return pageInfo;
    }

    @Transactional
    @Override
    public Integer bannedUser(Integer uid) {
        User user = userMapper.selectById(uid);
        BannedUser bannedUser = new BannedUser();
        bannedUser.setOpenid(user.getOpenid());
        bannedUser.setUnionid(user.getUnionid());
        bannedUserMapper.insert(bannedUser);
        return bannedUser.getId();
    }

    @Transactional
    @Override
    public void clearUserInfo(Integer uid) {

        // 1.删除菜品
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<Dish>();
        dishQueryWrapper.eq("uid", uid);
        List<Dish> dishList = dishMapper.selectList(dishQueryWrapper);
        for(Dish dish : dishList){
            dishService.deleteDishInfo(dish.getId());
        }
        // 2. 删除菜品类别
        QueryWrapper<DishType> dishTypeQueryWrapper = new QueryWrapper<>();
        dishTypeQueryWrapper.eq("uid", uid);
        List<DishType> dishTypeList = dishTypeMapper.selectList(dishTypeQueryWrapper);
        for(DishType dishType : dishTypeList){
            dishTypeMapper.deleteById(dishType.getId());
        }

        // 删除厨房
        QueryWrapper<Kitchen> kitchenQueryWrapper = new QueryWrapper<>();
        kitchenQueryWrapper.eq("uid", uid);
        kitchenMapper.delete(kitchenQueryWrapper);

        // 删除小组信息
        QueryWrapper<FanUser> fanUserQueryWrapper = new QueryWrapper<>();
        fanUserQueryWrapper.eq("uid", uid);
        fanUserMapper.delete(fanUserQueryWrapper);

        // 删除头像
        User user = userMapper.selectById(uid);
        if(user!=null && !Objects.equals(user.getPortraitUrl(), "https://sanshipu.jianguijizhang.com/minio/fanquan/user/portraitDefault.png")){
            MinIoUtil.deleteFile(user.getPortraitUrl());
        }
        // 删除用户记录
        userMapper.deleteById(uid);

    }
}
